package com.hepinghospital.user.ui.kotlin

import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hepinghospital.user.R
import com.hepinghospital.user.base.BaseActivity
import com.hepinghospital.user.ui.activity.home.BookDailyDoctorByDateActivity
import com.hepinghospital.user.ui.activity.home.SearchDoctorActivity
import com.hepinghospital.user.ui.dialog.RulesDialogFragment
import com.hepinghospital.user.viewmodel.DailyRegisterViewModel
import kotlinx.android.synthetic.main.activity_daily_register.*
import kotlinx.android.synthetic.main.include_title.*

/**
 * 门诊预约
 *   挂号分类列表
 */

class DailyRegisterActivity : BaseActivity() {

    val viewModel by lazy { ViewModelProviders.of(this).get(DailyRegisterViewModel::class.java) }
    lateinit var adapter: MyAdapter
    private lateinit var adapterRight: MyAdapterRight

    override fun initViews() {
        tv_title.text = "门诊预约"
        val layoutManager = LinearLayoutManager(this)
        adapter = MyAdapter()
        department_recyclerView.layoutManager = layoutManager
        department_recyclerView.adapter = adapter

        val layoutManager2 = LinearLayoutManager(this)
        adapterRight = MyAdapterRight()
        recyclerView.layoutManager = layoutManager2
        recyclerView.adapter = adapterRight

        viewModel.dataChanged.observe(this, Observer {
            if (viewModel.isFirst) {
                viewModel.setListCheck(viewModel.myList, 0)
                viewModel.getDepartmentList("1", if (viewModel.myList.isEmpty()) "" else viewModel.myList[0].id, "", viewModel.myListRight)
            }
            adapter.notifyDataSetChanged()
            adapterRight.notifyDataSetChanged()
        })

        edt_search.setOnClickListener {
            val bundle = Bundle()
            bundle.putInt("type", 4)
            startActivity(SearchDoctorActivity::class.java, bundle)
        }

        btn_must_know.setOnClickListener {
            showRulesDialogFragment()
        }
    }

    private lateinit var mRulesDialogFragment: RulesDialogFragment

    private fun showRulesDialogFragment() {
        mRulesDialogFragment = RulesDialogFragment.newInstance("8")
        mRulesDialogFragment.setmListener { mRulesDialogFragment.dismiss() }
        mRulesDialogFragment.show(supportFragmentManager, "mSchudeleDialogFragment")
    }


    override fun initData() {
        viewModel.getDepartmentList("0", "", "", viewModel.myList)
    }

    override fun getLayoutResource(): Int {
        return R.layout.activity_daily_register
    }


    inner class MyAdapter : RecyclerView.Adapter<MyAdapter.Holder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapter.Holder {
            val view = LayoutInflater.from(this@DailyRegisterActivity)
                .inflate(R.layout.item_department_left, parent, false)
            return Holder(view)
        }

        override fun getItemCount(): Int {
            return viewModel.myList.size
        }

        override fun onBindViewHolder(holder: MyAdapter.Holder, position: Int) {
            if (viewModel.myList[position].flag) {
                holder.head.visibility = View.VISIBLE
                holder.tvName.run {
                    setTextColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.commonBlack))
                    setBackgroundColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.white))
                    typeface = Typeface.DEFAULT_BOLD
                }

            } else {
                holder.head.visibility = View.INVISIBLE
                holder.tvName.run {
                    setTextColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.commonGrey))
//                    setBackgroundColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.backgroundGrey))
                    typeface = Typeface.DEFAULT
                }
            }
            holder.tvName.text = viewModel.myList[position].name

            holder.itemView.setOnClickListener {
                viewModel.setListCheck(viewModel.myList, position)
                viewModel.getDepartmentList("1", viewModel.myList[position].id, "", viewModel.myListRight)
                notifyDataSetChanged()
            }
        }

        inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val tvName: TextView = itemView.findViewById(R.id.tv_name)
            val head: View = itemView.findViewById<View>(R.id.head)
        }
    }

    inner class MyAdapterRight : RecyclerView.Adapter<MyAdapterRight.Holder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyAdapterRight.Holder {
            val view = LayoutInflater.from(this@DailyRegisterActivity)
                .inflate(R.layout.item_department_right, parent, false)
            return Holder(view)
        }

        override fun getItemCount(): Int {
            return viewModel.myListRight.size
        }

        override fun onBindViewHolder(holder: MyAdapterRight.Holder, position: Int) {
            if (viewModel.myListRight[position].flag) {
                holder.tv_name.setTextColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.color_5481fa))
            } else {
                holder.tv_name.setTextColor(ContextCompat.getColor(this@DailyRegisterActivity, R.color.commonGrey))
            }
            holder.tv_name.text = viewModel.myListRight[position].name

            holder.itemView.setOnClickListener {
                viewModel.setListCheck(viewModel.myListRight, position)
                notifyDataSetChanged()
                val bundle = Bundle()
                bundle.putString("id", viewModel.myListRight.get(position).id)
                bundle.putString("depName", viewModel.myListRight.get(position).name)
                startActivity(BookDailyDoctorByDateActivity::class.java, bundle)
            }
        }

        inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val tv_name: TextView = itemView.findViewById(R.id.tv_name)
        }
    }
}